package rsa

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/base64"
)

func ECBPKCS1PaddingEncrypt(raw []byte, pubder string) ([]byte, error) {
	der, err := base64.StdEncoding.DecodeString(pubder)
	if err != nil {
		return nil, err
	}
	pubI, err := x509.ParsePKIXPublicKey(der)
	if err != nil {
		return nil, err
	}
	pub := pubI.(*rsa.PublicKey)
	return rsa.EncryptPKCS1v15(rand.Reader, pub, raw)
}

func ECBPKCS1PaddingDecrypt(raw []byte, prider string) ([]byte, error) {
	der, err := base64.StdEncoding.DecodeString(prider)
	if err != nil {
		return nil, err
	}
	prkI, err := x509.ParsePKCS8PrivateKey(der)
	if err != nil {
		return nil, err
	}
	priv := prkI.(*rsa.PrivateKey)
	return rsa.DecryptPKCS1v15(rand.Reader, priv, raw)
}
